//
//  NewsViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewsViewController.h"


#import "CustomTableViewCell.h"
#import "NewsDetailViewController.h"
#import "AFNetworking.h"
#import "NewsESRootClass.h"
#import "Newsesarray.h"
#import "TopCustomTableViewCell.h"
#import "CustomHeaderView.h"
#import "ColumnsViewController.h"
#import "TAMacro.h"
#import "MJRefresh.h"

@interface NewsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic,strong) CustomHeaderView *customHeaderView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong) NewsESRootClass *newsESRootClass;
@property (nonatomic,strong) NSMutableArray<Newsesarray *> *newsArray;

@property (nonatomic,assign) NSInteger count;
@end

@implementation NewsViewController
#pragma mark -----视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self createNavigationItem];
    [self createTableView];
    [self newsNetworkGet];
    
  
}

- (void)createNavigationItem
{
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TitleLogo"]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ButtonList"] style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonClicked)];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.tableHeaderView = self.headView;
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"tableCell"];
    [_tableView registerClass:[TopCustomTableViewCell class] forCellReuseIdentifier:@"topTableCell"];
//    [self pullDownRefresh];
//    [self setUpRefresh];
}

#pragma mark ------左侧按钮的点击方法
- (void)leftButtonClicked
{
    ColumnsViewController *columnsVC = [ColumnsViewController new];
    [self.navigationController pushViewController:columnsVC animated:YES];
}


- (void)pullDownRefresh
{
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadNewData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/magazine/zh-hans?page=1&ver=iphone&app_ver=15";
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        _newsArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Newsesarray *newsesarray = [Newsesarray modelWithDic:dic];
            [_newsArray addObject:newsesarray];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self setUpRefresh];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];

}



//加载
- (void)setUpRefresh
{
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)];
//    [self.tableView.mj_footer beginRefreshing];
}


- (void)loadMoreBills
{
    _count = 1;
    _count += 1;
    
    NSString *str1 = @"http://watch-cdn.idailywatch.com/api/list/magazine/zh-hans?page=";
    NSString *str2 = [str1 stringByAppendingString:[NSString stringWithFormat:@"%ld",_count]];
    NSString *str3 = [str2 stringByAppendingString:@"&ver=iphone&app_ver=15"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = str3;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        _newsArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Newsesarray *newsesarray = [Newsesarray modelWithDic:dic];
            [_newsArray addObject:newsesarray];
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}




#pragma mark 网络请求
- (void)newsNetworkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/magazine/zh-hans?page=1&ver=iphone&app_ver=15";
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        _newsArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Newsesarray *newsesarray = [Newsesarray modelWithDic:dic];
            [_newsArray addObject:newsesarray];
    }
        [self.tableView reloadData];
//        [self.tableView.mj_footer endRefreshing];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}

#pragma mark -----tableview的item的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 1) {
        return _newsArray.count - 2;
    } else {
        return 1;
    }
}
#pragma mark -----返回cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
    
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];

    cell.titleLabel.text = _newsArray[indexPath.row + 1].title;
    cell.pubdateLabel.text = _newsArray[indexPath.row + 1].pubdate;
    cell.summaryLabel.text = _newsArray[indexPath.row + 1].summary;
        [cell.cover_thumb_hdImageView sd_setImageWithURL:[NSURL URLWithString:_newsArray[indexPath.row + 1].cover_thumb_hd]];

        
    [_customHeaderView.imageView1 sd_setImageWithURL:[NSURL URLWithString:_newsArray[0].cover_thumb_hd]];
    _customHeaderView.label1.text = _newsArray[0].title;
    return cell;
    } else {
        TopCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topTableCell"];
        [cell.cover_thumb_hdImageView sd_setImageWithURL:[NSURL URLWithString:_newsArray[0].cover_thumb_hd]];

        cell.titleLabel.text = _newsArray[indexPath.row].title;
//        [self setUpRefresh];
        return cell;
    }
}

#pragma mark -----tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetailViewController *ndvc = [NewsDetailViewController new];
    if (indexPath.section == 0) {
        ndvc.rowValue = indexPath.row;
    } else {
        ndvc.rowValue = indexPath.row + 1;
    }
    [self.navigationController pushViewController:ndvc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return SCREEN_WIDTH * 250 / 375;
    } else {
        return SCREEN_WIDTH * 140 / 375;
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
